package com.longdaji.decoration.ui.activitiesOfMine.collection;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.R;
import com.longdaji.decoration.adapter.CollectionAdapter;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.bean.Collection;
import com.longdaji.decoration.ui.detail.GoodsDetailActivity;
import com.longdaji.decoration.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CollectionActivity extends BaseActivity implements CollectionContract.View {

    private static final String TAG = CollectionActivity.class.getSimpleName();
    @Inject
    CollectionContract.Presenter mPresenter;
    @BindView(R.id.tv_tool_title)
    TextView tvTitle;
    @BindView(R.id.tv_finish)
    TextView tvFinish;
    @BindView(R.id.rv_collection)
    RecyclerView rvCollection;
    @BindView(R.id.ll_select_all)
    LinearLayout llSelectAll;
    @BindView(R.id.tv_delete)
    TextView tvDelete;
    @BindView(R.id.rl_manage_bar)
    RelativeLayout rlManageBar;


    private List<Collection.CollectionGoodsListBean> mCollectionList = new ArrayList<>();
    private CollectionAdapter mAdapter;
    private int[] dotState;
    private boolean isSelectAll = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection);
        setUnBinder(ButterKnife.bind(this));

        if (mPresenter != null)
            mPresenter.setView(this);
        tvTitle.setText("收藏");
        tvFinish.setText("管理");

        initView();

    }

    public static Intent getStartIntent(Context context) {
        Intent intent = new Intent(context, CollectionActivity.class);
        return intent;
    }

    private void initView() {
        mPresenter.initList(Account.getInstance().getUserid());
        rvCollection.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        mAdapter = new CollectionAdapter(mCollectionList);
        rvCollection.setAdapter(mAdapter);
        mAdapter.setOnViewClickListener(new CollectionAdapter.ViewListener() {
            @Override
            public void onViewClickNormal(int position) {
                if (dotState[position] == 0) {
                    dotState[position] = 1;
                } else {
                    dotState[position] = 0;
                }

            }

            @Override
            public void onViewClickManage(Collection.CollectionGoodsListBean item) {
                LogUtil.d(TAG, "CollectionGoodsListBean is " + item);
                Intent intent = GoodsDetailActivity.getStartIntent(CollectionActivity.this);
                intent.putExtra("goods_id", item.getGoodsId()+"");
                startActivity(intent);

            }
        });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }

    @OnClick({R.id.rl_tool_back, R.id.tv_finish, R.id.tv_delete, R.id.ll_select_all})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_tool_back:
                finish();
                break;
            case R.id.tv_finish:
//                mPresenter.onFinishClick();
                onManageClick();
                break;
            case R.id.tv_delete:
//                mPresenter.onDeleteClick();
                onDeleteClick();
                break;
            case R.id.ll_select_all:
//                mPresenter.onSelectClick();
                onSelectAllClick();
                break;
            default:
                break;
        }
    }

    private void onManageClick() {
        if (Constants.CLICK_MODE_NORMAL == mAdapter.getClickMode()) {
            tvFinish.setText("完成");
            mAdapter.setClickMode(Constants.CLICK_MODE_MANAGE);
            mAdapter.showAllIvDot(true, false);
            rlManageBar.setVisibility(View.VISIBLE);
            initDotState();
            //重置全选
            isSelectAll = false;
        } else if (Constants.CLICK_MODE_MANAGE == mAdapter.getClickMode()) {
            tvFinish.setText("管理");
            mAdapter.setClickMode(Constants.CLICK_MODE_NORMAL);
            mAdapter.showAllIvDot(false, false);
            rlManageBar.setVisibility(View.INVISIBLE);
        }
    }

    private void onDeleteClick() {
        StringBuilder sb = new StringBuilder();
        //标记是否删除了任何item
        boolean isAnyDelete = false;
        //根据dotState的记录，删除mCollectionList中对应的item，注意这里删除的是i-j，防止list越界
        for (int i = 0; i < dotState.length; i++) {
            if (dotState[i] == 1) {
                if (!isAnyDelete) {
                    isAnyDelete = true;
                }
                if (sb.toString().length() != 0) {
                    sb.append(",").append(mCollectionList.get(i).getGoodsId());
                } else {
                    sb.append(mCollectionList.get(i).getGoodsId());
                }
            }
        }
        mPresenter.doDeleteCollection(sb.toString(), Account.getInstance().getUserid());
        if (isAnyDelete) {
            Toast.makeText(this, "取消收藏成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "请先选择宝贝~", Toast.LENGTH_SHORT).show();
        }


//        //标记是否删除了任何item
//        boolean isAnyDelete = false;
//        //根据dotState的记录，删除mCollectionList中对应的item，注意这里删除的是i-j，防止list越界
//        for (int i = 0, j = 0; i < dotState.length; i++) {
//            if (dotState[i] == 1) {
//                if (!isAnyDelete) {
//                    isAnyDelete = true;
//                }
//                mCollectionList.remove(i - j);
//                j++;
//            }
//        }
//        //每次删除后更新dotState数组(只能重建)
//        mAdapter.notifyDataSetChanged();
//        initDotState();
//        if (isAnyDelete) {
//            Toast.makeText(this, "取消收藏成功", Toast.LENGTH_SHORT).show();
//        } else {
//            Toast.makeText(this, "请先选择宝贝~", Toast.LENGTH_SHORT).show();
//        }

    }


    private void onSelectAllClick() {
        //遍历此时的dotState，如果有任何一个未被选中，那么设置isSelectAll为true，否则为false；
        Log.d("mytest", "onSelectAllClick");
        int j = 0;
        isSelectAll = false;
        while (j < dotState.length) {
            if (dotState[j] == 0) {
                isSelectAll = true;
                break;
            }
            j++;
        }

        Log.d("mytest", "isSelectAll : " + isSelectAll);
        dotState = new int[mCollectionList.size()];
        if (isSelectAll) {
            isSelectAll = false;
            mAdapter.showAllIvDot(true, true);
            for (int i = 0; i < dotState.length; i++) {
                dotState[i] = 1;
            }
        } else {
            isSelectAll = true;
            mAdapter.showAllIvDot(true, false);
            for (int i = 0; i < dotState.length; i++) {
                dotState[i] = 0;
            }
        }
    }

    private void initDotState() {
        dotState = new int[mCollectionList.size()];
        for (int i = 0; i < dotState.length; i++) {
            dotState[i] = 0;
        }
    }

    @Override
    public void showGetCollectionSucceed(List<Collection.CollectionGoodsListBean> collectionGoodsBeanList) {
        mAdapter.setData(collectionGoodsBeanList);
        initDotState();
//        idList.clear();
    }

    @Override
    public void showDeleteCollectionSucceed() {

    }
}
